﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LoginUI : UIBase
{

    [Header("服务器Ip地址")]
    public InputField ServerIpInput;

    [Header("服务器端口")]
    public InputField ServerPortInput;

    [Header("登录")]
    public Button LoginBtn;

    [Header("玩家账号")]
    public InputField PlayerAccountInput;

    [Header("错误信息")]
    public Text ErrorMsgText;

    string userCount;

    // Use this for initialization
    void Start()
    {

        string serIp = PlayerPrefs.GetString("ServerIp");

        string serPort = PlayerPrefs.GetString("ServerPort");

        userCount = PlayerPrefs.GetString("PlayerAccount");

        if (string.IsNullOrEmpty(serIp))
        {

            serIp = GameConst.SocketAddress;

            serPort = GameConst.SocketPort.ToString();

            PlayerPrefs.SetString("ServerIp", serIp);

            PlayerPrefs.SetString("ServerPort", serPort);

        }

        if (userCount != null)
        {

            PlayerAccountInput.text = userCount;

        }

        ServerIpInput.text = serIp;

        ServerPortInput.text = serPort;

        LoginBtn.onClick.AddListener(() =>
        {

            if (string.IsNullOrEmpty(ServerIpInput.text))
            {

                ErrorMsgText.text = "请输入账号";

                return;

            }

            GameConst.SocketAddress = ServerIpInput.text;

            GameConst.SocketPort = int.Parse(ServerPortInput.text);

            if (!string.IsNullOrEmpty(ServerIpInput.text) && !string.IsNullOrEmpty(ServerPortInput.text))
            {

                PlayerPrefs.SetString("ServerIp", serIp);

                PlayerPrefs.SetString("ServerPort", serPort);

                PlayerPrefs.SetString("PlayerAccount", userCount);

                //连接服务器
                Net.NetManager.Instance.SendConnect();

            }

        });

        PlayerAccountInput.onValueChanged.AddListener((account) =>
        {

            if (string.IsNullOrEmpty(account))
            {

                return;

            }

            userCount = account;

            PlayerPrefs.SetString("PlayerAccount", userCount);
                
        });

        GameMassage.GameMessageCenter.instance.RegisterGameMsg(GameMassage.MessageId.CONNECT_SUCCESS, ConnectSuccess);

        Net.NetManager.Instance.RegisterNetMsg(typeof(Msg.LoginFaild), LoginFailed);

        Net.NetManager.Instance.RegisterNetMsg(typeof(Msg.LoginSuccessfull), LoginSuccess);

    }

    // Update is called once per frame
    void Update()
    {

    }

    /// <summary>
    /// 服务器连接成功
    /// </summary>
    /// <param name="obj"></param>
    void ConnectSuccess(object obj)
    {

        //服务器连接成功
        //验证账号密码
        Msg.Login login = new Msg.Login();

        login.Account = userCount;

        Net.NetManager.Instance.SendMessage(login);

    }

    /// <summary>
    /// 登录成功
    /// </summary>
    /// <param name="obj"></param>
    void LoginSuccess(object obj)
    {

        Debug.Log("获取服务器列表");

    }

    /// <summary>
    /// 登录失败
    /// </summary>
    /// <param name="obj"></param>
    void LoginFailed(object obj)
    {

        Debug.Log("登录失败");

        Net.NetManager.Instance.OnRemove();

        Msg.LoginFaild faild = obj as Msg.LoginFaild;

        switch (faild.Code)
        {

            case Msg.LoginFaild.Types.ErrorCode.AccIdinvalid:

                Debug.Log("账号无效");

                ErrorMsgText.text = "账号无效";

                break;
            case Msg.LoginFaild.Types.ErrorCode.AccountOrPasswardNotMatch: ;

                Debug.Log("账号或密码无效");

                break;

            case Msg.LoginFaild.Types.ErrorCode.InnerError:


                break;

            case Msg.LoginFaild.Types.ErrorCode.LoginRepeat:

                Debug.Log("登录重复");

                break;

        }

    }


}
